global pathname "__your path here__"
cd "$pathname"

log using "replication_log.log", replace



* trends data

import excel "jhu 2020 covid data.xlsx", sheet("Sheet1") firstrow clear
collapse (sum) E-SV, by(CountryRegion)
keep if CountryRegion=="US" | CountryRegion=="United Kingdom" | CountryRegion=="Brazil" | CountryRegion=="Italy" | CountryRegion=="China" | CountryRegion=="India"
replace CountryRegion="UK" if CountryRegion=="United Kingdom"

local i = 1
foreach v of var E-SV {
         rename `v' v`i'
		 local i = `i' + 1
}
reshape long v, i(CountryRegion) j(var)
reshape wide v, i(var) j(CountryRegion) string
rename v* *
rename ar date
tsset date
format date %td
replace date = date+21935


foreach v of var Brazil-US {
	replace `v' = `v'/1000
	gen change_`v' = `v'-`v'[_n-1]
	tssmooth ma change_`v'_sm = change_`v', window(2 1 2)
}



twoway (tsline change_Brazil_sm, lpattern(dash)) ///
	(tsline change_China_sm, lpattern(longdash) lcolor(black)) ///
	(tsline change_Italy_sm, ) ///
	(tsline change_UK_sm, lcolor(black)) ///
	(tsline change_US_sm, lpattern(dot) scheme(tpepbw) ///
	ttext(20 01feb2020 "China") ///
	ttext(200 01feb2021 "USA") ///
	ttext(75 01jul2021 "Brazil") ///
	ttext(32 30mar2021 "Italy") ///
	ttext(32 05feb2021 "UK") ///
	tlabel(01mar2020 01may2020 01jul2020 01sep2020 01nov2020 01jan2021 01mar2021 01may2021 01jul2021, angle(forty_five) format("%tdMon-YY")) ///
	xtitle("") ytitle("New Cases (1000s)") legend(off))
graph export "figures/S1.pdf", as(pdf) replace
graph export "figures/S1.png", as(png) replace

 
 
use "survey.dta", clear

gen aia = (iai*-1)+6

gen feelingthermometer = .
replace feelingthermometer = (q137_4+q137_5+q137_6)/3 if race==1
replace feelingthermometer = (q137_3+q137_5+q137_6)/3 if race==2
replace feelingthermometer = (q137_3+q137_4+q137_6)/3 if race==3
replace feelingthermometer = feelingthermometer*-1


local balance_vars "gender agecat race marstat educ income rural inputstate democrat racialresentment"
matrix drop _all

foreach var of local balance_vars {

	

	
	qui tabulate `var' entry_exp, chi
	local p = round(r(p),.001)		
	di `"`var' "' `p'
	
	matrix resmat = nullmat(resmat) \ `p'
		
}

matrix rownames resmat = `balance_vars'
matrix colnames resmat =  Entry
matrix list resmat

forvalues i = 3/6 {
	
	foreach var of local balance_vars {

	qui tabulate `var' banentryexp_w`i', chi
	local p = round(r(p),.001)		
	di `"`var' "' `p'
	
	matrix resmat_w`i' = nullmat(resmat_w`i') \ `p'
		
	}
	
	matrix list resmat_w`i'

}



putexcel set "tables/S1.xlsx", sheet("sheet1") replace
putexcel A1=matrix(resmat), names
putexcel C2=matrix(resmat_w3)
putexcel D2=matrix(resmat_w4)
putexcel E2=matrix(resmat_w5)
putexcel F2=matrix(resmat_w6)

cd "${pathname}/tables"
asdoc sum gender agecat race marstat educ income  rural democrat racialresentment if weight_w1!=., save(S9.doc) replace
asdoc tabulate inputstate if weight_w1!=., save(S9.doc) append
asdoc tabulate democrat racialresentment if weight_w1!=., save(S16.doc) replace
cd "${pathname}"

* validate RR measure

eststo valid: reg feelingthermometer i.racialresentment, robust
esttab valid using "tables/S3.csv", b(3) se(3) wide label nobaselevels csv replace





* WAVE 1 BAN ENTRY EXPERIMENT



gen entry_exp_recode = .
replace entry_exp_recode = 1 if entry_exp==2
replace entry_exp_recode = 2 if entry_exp==1
replace entry_exp_recode = 3 if entry_exp==3
label define entry_exp_recode 1 "Britain" 2 "China" 3 "Italy"
label values entry_exp_recode entry_exp_recode

summarize banentry_w1
local mean_b = r(mean)

summarize aia, detail
local aia_median = r(p50)

summarize racialresentment, detail
local race_median = r(p50)



* SATE estimates
eststo sate_w1: reg banentry_w1 i.entry_exp_recode  [pweight=weight], robust
eststo sate_w1_c: reg banentry_w1 i.entry_exp_recode  i.agecat gender white married i.educ i.income i.inputstate i.rural [pweight=weight], robust
esttab sate_w1 sate_w1_c using "tables/S4.csv", b(3) se(3) wide label nobaselevels csv replace

margins entry_exp_recode
preserve
matrix preds=r(table)
matrix b = preds[1, 1 .. 3]'
matrix se = preds[2, 1 .. 3]'
svmat b
replace b1 = b1[_n-2] in 5
replace b1 = b1[_n-1] in 3
replace b1 = . in 2
svmat se
replace se1 = se1[_n-2] in 5
replace se1 = se1[_n-1] in 3
replace se1 = . in 2
gen hi = b1+1.96*se1
gen lo = b1-1.96*se1
gen country = _n in 1/5

twoway (bar b1 country) (rspike hi lo country, lwidth(medium) color(black) ///
	xlabel( 1 "Great Britain" 3 "China" 5 "Italy", noticks) xtitle("") ylabel(1(1)5) legend(off) ytitle("Support Entry Ban (1-5)"))
graph export "figures/1.png", as(png) replace
graph export "figures/1.pdf", as(pdf) replace

restore



* CATE estimates

eststo cates_w1: reg banentry_w1 i.entry_exp_recode##i.democrat##c.racialresentment  i.agecat gender white married i.educ i.income i.inputstate i.rural [pweight=weight], robust
esttab cates_w1, b(3) se(3) wide label nobaselevels
esttab cates_w1 using "tables/S5.csv", b(3) se(3) wide label nobaselevels csv replace


tab entry_exp_recode democrat if racialresentment <= `race_median'
margins entry_exp_recode if racialresentment<=`race_median', at(democrat=(0 1 2))
preserve
matrix preds=r(table)
matrix b = preds[1, 1 .. 9]'
matrix se = preds[2, 1 .. 9]'
g at_race = 1 if mod(_n,3)==1
replace at_race = 2 if mod(_n,3)==2
replace at_race = 3 if mod(_n,3)==0
label define at_race 1 "British" 2 "Chinese" 3 "Italian", replace
label values at_race at_race
g at_dem = 0 in 1/3
replace at_dem = 1 in 4/6
replace at_dem = 2 in 7/9
svmat b
svmat se
gen hi = b1+1.96*se1
gen lo = b1-1.96*se1
gen demrace = at_race if at_dem == 0
replace demrace = at_race + 4 if at_dem == 1
replace demrace = at_race + 8 if at_dem == 2
twoway (bar b1 demrace if at_race==1, ylabel(3(1)5) color(red))  (bar b1 demrace if at_race==2, color(blue)) (bar b1 demrace if at_race==3, color(gs12)) ///
	(rspike hi lo demrace, lwidth(medium) color(black)), ///
	xlabel( 2 "R" 6 "D" 10 "O", noticks) xtitle("") legend(row(1) order(1 "Great Britain" 2 "China" 3 "Italy") )  ///
	ytitle("Support Entry Ban (1-5)") yline(`mean_b', lpattern(dash)) name(imm_hi, replace) title("Low Racial Resentment Score")
restore

tab entry_exp_recode democrat if racialresentment > `race_median'
margins entry_exp_recode if racialresentment > `race_median', at(democrat=(0 1 2))
preserve
matrix preds=r(table)
matrix b = preds[1, 1 .. 9]'
matrix se = preds[2, 1 .. 9]'
g at_race = 1 if mod(_n,3)==1
replace at_race = 2 if mod(_n,3)==2
replace at_race = 3 if mod(_n,3)==0
label define at_race 1 "British" 2 "Chinese" 3 "Italian", replace
label values at_race at_race
g at_dem = 0 in 1/3
replace at_dem = 1 in 4/6
replace at_dem = 2 in 7/9
svmat b
svmat se
gen hi = b1+1.96*se1
gen lo = b1-1.96*se1
gen demrace = at_race if at_dem == 0
replace demrace = at_race + 4 if at_dem == 1
replace demrace = at_race + 8 if at_dem == 2
twoway (bar b1 demrace if at_race==1, ylabel(3(1)5) color(red))  (bar b1 demrace if at_race==2, color(blue)) (bar b1 demrace if at_race==3, color(gs12)) ///
	(rspike hi lo demrace, lwidth(medium) color(black)), ///
	xlabel( 2 "R" 6 "D" 10 "O", noticks) xtitle("") legend(row(1) order(1 "Great Britain" 2 "China" 3 "Italy") )  ///
	ytitle("Support Entry Ban (1-5)") yline(`mean_b', lpattern(dash)) name(imm_lo, replace)  title("High Racial Resentment Score")
restore


margins entry_exp_recode, at(democrat=(0 1 2))
preserve
matrix preds=r(table)
matrix b = preds[1, 1 .. 9]'
matrix se = preds[2, 1 .. 9]'
g at_race = 1 if mod(_n,3)==1
replace at_race = 2 if mod(_n,3)==2
replace at_race = 3 if mod(_n,3)==0
label define at_race 1 "British" 2 "Chinese" 3 "Italian", replace
label values at_race at_race
g at_dem = 0 in 1/3
replace at_dem = 1 in 4/6
replace at_dem = 2 in 7/9
svmat b
svmat se
gen hi = b1+1.96*se1
gen lo = b1-1.96*se1
gen demrace = at_race if at_dem == 0
replace demrace = at_race + 4 if at_dem == 1
replace demrace = at_race + 8 if at_dem == 2
twoway (bar b1 demrace if at_race==1, ylabel(3(1)5) color(red))  (bar b1 demrace if at_race==2, color(blue)) (bar b1 demrace if at_race==3, color(gs12)) ///
	(rspike hi lo demrace, lwidth(medium) color(black)), ///
	xlabel( 2 "R" 6 "D" 10 "O", noticks) xtitle("") legend(row(1) order(1 "Great Britain" 2 "China" 3 "Italy") )  ///
	ytitle("Support Entry Ban (1-5)") yline(`mean_b', lpattern(dash)) name(race_all, replace)  title("All Respondents")
restore

grc1leg imm_hi imm_lo, name(hilo, replace) 
grc1leg hilo race_all, rows(2) note("Note: The dashed horizontal line is the mean response across all respondents in the sample.")
graph export "figures/2.png", as(png) replace
graph export "figures/2.pdf", as(pdf) replace




* FOUR WAVES 

preserve

import excel "country_data.xlsx", sheet("Sheet1") firstrow clear
gen banentryexp_w=1 if location=="China"
replace banentryexp_w=2 if location=="Brazil"
replace banentryexp_w=3 if location=="United Kingdom"
gen  treatwaveid = wave*10+banentryexp_w

drop wave date
sort treatwaveid 

save country_data.dta, replace

restore


preserve 

keep banentryexp_w* banentry_w* democrat_w* caseid weight* aia racialresentment agecat gender white married educ income inputstate rural
drop banentryexp_w1 banentryexp_w2 banentry_w1 banentry_w2
drop weight

reshape long banentryexp_w banentry_w democrat_w weight_w, i(caseid) j(wave)
label values democrat democrat

xtset caseid wave
gen  treatwaveid = wave*10+banentryexp_w
sort treatwaveid
merge m:1 treatwaveid using country_data.dta
drop if wave==.
drop _merge


graph drop _all



* SATE Analysis
eststo sate_w2to5: xtreg banentry_w ib3.banentryexp_w, re robust
eststo sate_w2to5_f: xtreg banentry_w ib3.banentryexp_w, fe robust
eststo sate_w2to5_c: xtreg banentry_w ib3.banentryexp_w i.agecat gender white married i.educ i.income i.inputstate i.rural, re robust
esttab sate_w2to5 sate_w2to5_c using "tables/S6.csv", b(3) se(3) wide label nobaselevels csv replace

* CATE estimates by wave
summarize racialresentment, detail
local race_median = r(p50)

replace wave = wave-1

eststo cates_w2to5: xtreg banentry_w i.banentryexp_w##i.wave##i.democrat_w##c.racialresentment  i.agecat gender white married i.educ i.income i.inputstate i.rural, re robust
esttab cates_w2to5, b(3) se(3) wide label nobaselevels
esttab cates_w2to5 using "tables/S7.csv", b(3) se(3) wide label nobaselevels csv replace

margins banentryexp_w if democrat==0 & racialresentment <= `race_median', at(wave=(2 3 4 5)) ///
	plot(recastci(rarea) ciopts(color(gs12)) legend(rows(1)) xtitle("Wave") title("Republicans") name(rep1, replace) ylabel(1(1)5)) 
margins banentryexp_w if democrat==0 & racialresentment > `race_median', at(wave=(2 3 4 5)) ///
	plot(recastci(rarea) ciopts(color(gs12)) legend(rows(1)) xtitle("Wave") title("Republicans") name(rep2, replace) ylabel(1(1)5))


margins banentryexp_w if democrat==1 & racialresentment <= `race_median', at(wave=(2 3 4 5)) ///
	plot(recastci(rarea) ciopts(color(gs12)) legend(rows(1)) xtitle("Wave") title("Democrats") name(dem1, replace) ylabel(1(1)5))
margins banentryexp_w if democrat==1 & racialresentment > `race_median', at(wave=(2 3 4 5)) ///
	plot(recastci(rarea) ciopts(color(gs12)) legend(rows(1)) xtitle("Wave") title("Democrats") name(dem2, replace) ylabel(1(1)5))


margins banentryexp_w if democrat==2 & racialresentment <= `race_median', at(wave=(2 3 4 5)) ///
	plot(recastci(rarea) ciopts(color(gs12)) legend(rows(1)) xtitle("Wave") title("Others") name(oth1, replace) ylabel(1(1)5))
margins banentryexp_w if democrat==2 & racialresentment > `race_median', at(wave=(2 3 4 5)) ///
	plot(recastci(rarea) ciopts(color(gs12)) legend(rows(1)) xtitle("Wave") title("Others") name(oth2, replace) ylabel(1(1)5))

grc1leg rep1 dem1 oth1, rows(1) name(low, replace) title("Low RR")
grc1leg rep2 dem2 oth2, rows(1) name(high, replace) title("High RR")
grc1leg low high, rows(2)


graph export "figures/3.png", as(png) replace
graph export "figures/3.pdf", as(pdf) replace

restore 





preserve 

keep banentryexp_w* banentry_w* democrat_w* caseid weight* aia racialresentment agecat gender white married educ income inputstate rural
drop banentryexp_w1 banentryexp_w2 banentry_w1 banentry_w2
drop weight

reshape long banentryexp_w banentry_w democrat_w weight_w, i(caseid) j(wave)
label values democrat democrat

xtset caseid wave
gen  treatwaveid = wave*10+banentryexp_w
sort treatwaveid
merge m:1 treatwaveid using country_data.dta
drop if wave==.
drop _merge


graph drop _all


tempvar logcases
gen `logcases' = ln(new_cases_smoothed)

summarize racialresentment, detail
local race_median = r(p50)


eststo cates_cases_w2to5: xtreg banentry_w i.banentryexp_w##i.democrat_w##c.`logcases'##c.racialresentment  i.agecat gender white married i.educ i.income i.inputstate i.rural, re robust
esttab cates_cases_w2to5, b(3) se(3) wide label nobaselevels
esttab cates_cases_w2to5 using "tables/S8.csv", b(3) se(3) wide label nobaselevels csv replace



margins if aia<=`race_median', dydx(`logcases') at(banentryexp_w==(1 2 3) democrat==0) plot(ciopts(lwidth(medium))  recast(scatter) ///
	xtitle("") title("Republicans") name(low_r, replace) legend(off)  yline(0))
margins if aia>`race_median', dydx(`logcases') at(banentryexp_w==(1 2 3)  democrat==0) plot(ciopts(lwidth(medium))  recast(scatter) ///
	xtitle("") title("Republicans") name(high_r, replace) legend(off)  yline(0))


margins if aia<=`race_median', dydx(`logcases') at(banentryexp_w==(1 2 3) democrat==1) plot(ciopts(lwidth(medium))  recast(scatter) ///
	xtitle("") title("Democrats") name(low_d, replace) legend(off)  yline(0))
margins if aia>`race_median', dydx(`logcases') at(banentryexp_w==(1 2 3)  democrat==1) plot(ciopts(lwidth(medium))  recast(scatter) ///
	xtitle("") title("Democrats") name(high_d, replace) legend(off)  yline(0))


margins if aia<=`race_median', dydx(`logcases') at(banentryexp_w==(1 2 3) democrat==2) plot(ciopts(lwidth(medium))  recast(scatter) ///
	xtitle("") title("Others") name(low_o, replace) legend(off)  yline(0))
margins if aia>`race_median', dydx(`logcases') at(banentryexp_w==(1 2 3)  democrat==2) plot(ciopts(lwidth(medium))  recast(scatter) ///
	xtitle("") title("Others") name(high_o, replace) legend(off)  yline(0))

graph combine low_r low_d low_o, rows(1) name(low, replace) title("Low RR") ycommon
graph combine high_r high_d high_o, rows(1) name(high, replace) title("High RR") ycommon
graph combine low high, rows(2) ycommon

graph export "figures/4.png", as(png) replace
graph export "figures/4.pdf", as(pdf) replace





restore





******* REPEAT THE WHOLE ANALYSIS FOR WHITE RESPONDENTS ONLY

use "survey.dta", clear

keep if white==1

gen aia = (iai*-1)+6

gen feelingthermometer = .
replace feelingthermometer = (q137_4+q137_5+q137_6)/3 if race==1
replace feelingthermometer = (q137_3+q137_5+q137_6)/3 if race==2
replace feelingthermometer = (q137_3+q137_4+q137_6)/3 if race==3
replace feelingthermometer = feelingthermometer*-1


* validate RR measure: WHITES ONLY

eststo valid: reg feelingthermometer i.racialresentment, robust
esttab valid using "tables/S10.csv", b(3) se(3) wide label nobaselevels csv replace




* WAVE 1 BAN ENTRY EXPERIMENT: WHITES ONLY



gen entry_exp_recode = .
replace entry_exp_recode = 1 if entry_exp==2
replace entry_exp_recode = 2 if entry_exp==1
replace entry_exp_recode = 3 if entry_exp==3
label define entry_exp_recode 1 "Britain" 2 "China" 3 "Italy"
label values entry_exp_recode entry_exp_recode

summarize banentry_w1
local mean_b = r(mean)

summarize aia, detail
local aia_median = r(p50)

summarize racialresentment, detail
local race_median = r(p50)



* SATE estimates:  WHITES ONLY 
eststo sate_w1: reg banentry_w1 i.entry_exp_recode  [pweight=weight], robust
eststo sate_w1_c: reg banentry_w1 i.entry_exp_recode  i.agecat gender white married i.educ i.income i.inputstate i.rural [pweight=weight], robust
esttab sate_w1 sate_w1_c using "tables/S11.csv", b(3) se(3) wide label nobaselevels csv replace



* CATE estimates: WHITES ONLY

eststo cates_w1: reg banentry_w1 i.entry_exp_recode##i.democrat##c.racialresentment  i.agecat gender white married i.educ i.income i.inputstate i.rural [pweight=weight], robust
esttab cates_w1, b(3) se(3) wide label nobaselevels
esttab cates_w1 using "tables/S12.csv", b(3) se(3) wide label nobaselevels csv replace




* FOUR WAVES: WHITES ONLY


preserve 

keep banentryexp_w* banentry_w* democrat_w* caseid weight* aia racialresentment agecat gender white married educ income inputstate rural
drop banentryexp_w1 banentryexp_w2 banentry_w1 banentry_w2
drop weight

reshape long banentryexp_w banentry_w democrat_w weight_w, i(caseid) j(wave)
label values democrat democrat

xtset caseid wave
gen  treatwaveid = wave*10+banentryexp_w
sort treatwaveid
merge m:1 treatwaveid using country_data.dta
drop if wave==.
drop _merge


graph drop _all



* SATE Analysis
eststo sate_w2to5: xtreg banentry_w ib3.banentryexp_w, re robust
eststo sate_w2to5_f: xtreg banentry_w ib3.banentryexp_w, fe robust
eststo sate_w2to5_c: xtreg banentry_w ib3.banentryexp_w i.agecat gender white married i.educ i.income i.inputstate i.rural, re robust
esttab sate_w2to5 sate_w2to5_c using "tables/S13.csv", b(3) se(3) wide label nobaselevels csv replace

* CATE estimates by wave
summarize racialresentment, detail
local race_median = r(p50)

replace wave = wave-1

eststo cates_w2to5: xtreg banentry_w i.banentryexp_w##i.wave##i.democrat_w##c.racialresentment  i.agecat gender white married i.educ i.income i.inputstate i.rural, re robust
esttab cates_w2to5, b(3) se(3) wide label nobaselevels
esttab cates_w2to5 using "tables/S14.csv", b(3) se(3) wide label nobaselevels csv replace

restore 




preserve 

keep banentryexp_w* banentry_w* democrat_w* caseid weight* aia racialresentment agecat gender white married educ income inputstate rural
drop banentryexp_w1 banentryexp_w2 banentry_w1 banentry_w2
drop weight

reshape long banentryexp_w banentry_w democrat_w weight_w, i(caseid) j(wave)
label values democrat democrat

xtset caseid wave
gen  treatwaveid = wave*10+banentryexp_w
sort treatwaveid
merge m:1 treatwaveid using country_data.dta
drop if wave==.
drop _merge


graph drop _all


tempvar logcases
gen `logcases' = ln(new_cases_smoothed)

summarize racialresentment, detail
local race_median = r(p50)


eststo cates_cases_w2to5: xtreg banentry_w i.banentryexp_w##i.democrat_w##c.`logcases'##c.racialresentment  i.agecat gender white married i.educ i.income i.inputstate i.rural, re robust
esttab cates_cases_w2to5, b(3) se(3) wide label nobaselevels
esttab cates_cases_w2to5 using "tables/S15.csv", b(3) se(3) wide label nobaselevels csv replace



restore



* clean up
rm country_data.dta


log close
